<template>
  <view class="message-container">
		<ImageItem v-if="type == 'img'" @previewImg="previewImg" :html="detail.html"></ImageItem>
		<ImageText v-if="type == 'img/text'" @previewImg="previewImg" :html="detail.html"></ImageText>
		<TextItem v-if="type == 'text'" :content="detail.content"></TextItem>
		<FileItem v-if="type == 'file' || type == 'dir'" :workerId="userInfo.worker_id" :message="message" :detail="detail"></FileItem>
		<FileUpdateItem v-if="type == 'update-file'" :workerId="userInfo.worker_id" :message="message" :detail="detail"></FileUpdateItem>
		<ApplyItem v-if="type == 'apply'" :detail="detail"></ApplyItem>
		<ApplyUrge v-if="type == 'apply_urge'" :detail="detail"></ApplyUrge>
		<ApplyMention v-if="type == 'apply_mention'" :detail="detail"></ApplyMention>
		<LogItem v-if="type == 'log'" :detail="detail"></LogItem>
		<NoticeItem v-if="type == 'notice'" :detail="detail"></NoticeItem>
    <logNotice v-if="type == 'log_notice'" :detail="detail"></logNotice>
    <logNotice v-if="type == 'log_sum_notice'" :detail="detail"></logNotice>
    <logNotice v-if="type == 'log_sum'" :detail="detail"></logNotice>
  </view>
</template>

<script>
import { chatApi } from '@/apis/cloud/chat'
import ImageItem from './MessageItem/ImageItem.vue'
import TextItem from './MessageItem/TextItem.vue'
import ImageText from './MessageItem/ImageText.vue'
import FileItem from './MessageItem/FileItem.vue'
import FileUpdateItem from './MessageItem/FileUpdateItem.vue'
import ApplyItem from './MessageItem/ApplyItem.vue'
import ApplyUrge from './MessageItem/ApplyUrge.vue'
import LogItem from './MessageItem/LogItem.vue'
import ApplyMention from './MessageItem/ApplyMention.vue'
import NoticeItem from './MessageItem/NoticeItem.vue'
import logNotice from './MessageItem/logNotice.vue'
import logSumNotice from './MessageItem/logSumNotice.vue'
import logSum from './MessageItem/logSum.vue'
export default {
  name: 'Message',
	components: {
		ImageItem,
		TextItem,
		ImageText,
		FileItem,
		FileUpdateItem,
		ApplyItem,
		ApplyUrge,
		ApplyMention,
		LogItem,
		NoticeItem,
    logNotice,
    logSumNotice,
    logSum
	},
  props: {
    message: {
      type: Object,
      required: true,
			default: {}
    },
		detail: {
		  type: Object,
		  required: true,
			default: {}
		},
		type: {
			type: String,
			require: true,
			default: {}
		},
		groupId: {
			type: Number,
		},
		dialogId: {
			type: Number
		},
  },
  data() {
		return {
			friend: {},
			imInfo: {},
			userInfo: {}
		}
	},
	async mounted() {
		// #ifdef H5
		this.imInfo = JSON.parse(await uni.getStorageSync('imInfo'));
		this.userInfo = JSON.parse(await uni.getStorageSync('user')).user;
		// #endif
		
		// #ifdef APP-PLUS
		this.imInfo = this.chatStore.imInfo || JSON.parse(uni.getStorageSync('imInfo'));// 当前登录用户
		this.userInfo = this.userStore.user;
		// #endif
	},
	methods: {
		async previewImg(url) {
			const params = {
				'data[account]': this.imInfo?.account,
				'data[token]': this.userInfo?.token,
				'data[bizId]': this.dialogId || this.groupId,
				'data[bizType]': this.dialogId ? 'friend' : 'group',
				'data[mid]': this.message.id,
				'data[size]': '100',
				url: 'index.php/users/message/getPreviewImgUrlList'
			}
			const msg = await chatApi(this.userInfo?.token, params);
			const urls = msg.data.map(item => {
				if (!(/^https?:\/\//i.test(item.url))) {
					item.url = "https://chat.mfarmers.com:88/"+item.url
				}
				item.url = item.url.replace('http:', 'https:').replace(':81', ':88');
				item.url = item.url.replace(/\/thumb_([^/]+\.(png|jpg|jpeg|gif|webp))/i, '/$1');
				return item.url
			});
			const cache = url.replace(/\/thumb_([^/]+\.(png|jpg|jpeg|gif|webp))/i, '/$1').replace('http:', 'https:').replace(':81', ':88');

			uni.previewImage({
				urls: urls,
				current: cache
			})
		},
	}
};
</script>

<style lang="scss" scoped>

</style>
